<?php
namespace app\service;
use alipay\AopClient;
use alipay\AlipaySystemOauthTokenRequest;//换取授权访问令牌
use alipay\AlipayOpenPublicFollowBatchqueryRequest;//获取关注用户列表
use alipay\AlipayOpenPublicMessageSingleSendRequest;//单发模板消息接口
use alipay\AlipayOpenPublicUserFollowQueryRequest;//查询用户是否关注信息
use alipay\AlipayTradeCreateRequest;//订单创建接口
use alipay\AlipayOpenPublicMenuCreateRequest;//默认菜单创建接口
use alipay\AlipayOpenPublicMenuModifyRequest;//默认菜单更新接口
use alipay\AlipayUserInfoShareRequest;//支付宝会员授权信息查询
use alipay\AlipayTradeOrderSettleRequest;//统一收单交易结算接口

use alipay\AlipayFundTransToaccountTransferRequest;//单笔转账到支付宝账户接口

use alipay\AlipayTradeRefundRequest;//退款接口
class lifeapiService{
    
    const PRIKEYPATH = '/www/wwwroot/www.zaozaopay.com/public/rsa/life/lifeprikey.txt';//私钥路径
    const PUBKEYPATH = '/www/wwwroot/www.zaozaopay.com/public/rsa/life/lifepubkey.txt';//支付宝公钥路径
    
    const PUBLICPRIKEYPATH = '/www/wwwroot/zaozao/prikey.txt';//私钥路径
    const PUBLICPUBKEYPATH = '/www/wwwroot/zaozao/pubkey.txt';//支付宝公钥路径
  
    private $app_id;//支付宝分配给开发者的应用
    private $method;//接口名称
    private $format;//仅支持JSON  no
    private $charset = 'UTF-8';//请求使用的编码格式，如utf-8,gbk,gb2312等
    private $sign_type = 'RSA2';//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
    private $sign;//商户请求参数的签名串  no
    private $timestamp;//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
    private $version = '1.0';//调用的接口版本，固定为：1.0
    private $app_auth_token;//详见应用授权概述 no
    private $biz_content;//请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档
    
    public function getPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2017071807800670';
        $aop->rsaPrivateKey = file_get_contents(self::PUBLICPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBLICPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
    
    //生活号应用
    public function lifegetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2017082608391103';//  2018112662334699
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey=file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
    
    //换取授权访问令牌
    public function oauthToken($code){
        $aop = $this->lifegetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ( $request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        }
        return $result->$responseNode;
    }

    //获取关注用户列表
    public function getuserlist($data){
        $data = json_decode($data);
        $aop = $this->lifegetPublicContent();
        $request = new AlipayOpenPublicFollowBatchqueryRequest ();
        $request->setBizContent("{" .
            "\"next_user_id\":\"$data->next_user_id\"" .
            "  }");

        $result = $aop->execute ($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

//----------------单发模板消息message.single.send(开始)---------------------

   
    
    //创建订单
    public function orderCreate($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeCreateRequest ();
        $request->setBizContent("{" .
        "\"out_trade_no\":\"$data->out_trade_no\"," .
        "\"total_amount\":$data->total_amount," .
        "\"subject\":\"$data->subject\"," .
        "\"buyer_id\":\"$data->buyer_id\"," .
        "\"extend_params\":{".
            "\"sys_service_provider_id\":\"2088621722997814\"".
            "},".
        "\"timeout_express\":\"15d\"" .
        "  }");
        $request->setNotifyUrl($data->notifyurl);
        
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    //统一收单交易结算接口
    public function orderSettle($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeOrderSettleRequest ();
        $request->setBizContent("{" .
        "\"out_request_no\":\"$data->out_order_no\"," .
        "\"trade_no\":\"$data->trade_no\"," .
        "      \"royalty_parameters\":[{" .
                "\"royalty_type\":\"transfer\"," .
                "\"trans_out\":\"$data->trans_out\"," .
                "\"trans_out_type\":\"$data->trans_out_type\"," .
                "\"trans_in_type\":\"$data->trans_in_type\"," .
                "\"trans_in\":\"$data->trans_in\"," .
                "\"amount\":\"$data->amount\"," .
                "\"desc\":\"$data->desc\"" .
                "        }]," .
        "\"operator_id\":\"A0001\"" .
        "  }");
        $result = $aop->execute ( $request); 
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }



    //(单笔转账到支付宝账户接口)
    public function accountTransfer($data){
        $data = json_decode($data);
        $aop = $this->lifegetPublicTranfer();//getPublicContent
        $request = new AlipayFundTransToaccountTransferRequest ();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"$data->out_biz_no\"," .
            "\"payee_type\":\"$data->payee_type\"," .
            "\"payee_account\":\"$data->payee_account\"," .
            "\"amount\":\"$data->amount\"," .
            "\"payer_show_name\":\"$data->payer_show_name\"," .
            "\"payee_real_name\":\"$data->payee_real_name\"," .
            "\"remark\":\"$data->remark\"" .
            "  }");
        /*$request->setBizContent("{" .
            "\"out_biz_no\":\"$data->out_biz_no\"," .
            "\"payee_type\":\"$data->payee_type\"," .
            "\"payee_account\":\"$data->payee_account\"," .
            "\"amount\":\"$data->amount\"" .
            "  }");*/
        $result = $aop->execute ($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }


    //code获取accessToken
    public function getuserToken(){
        $data = json_decode($data);
        $aop = $this->lifegetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode("4b203fe6c11548bcabd8da5bb087a83b");
        $request->setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        $result = $aop->execute ( $request); 

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
    }
    //accessToken换取用户信息                支付宝会员授权信息查询
    public function getuserinfo($accessToken){
        $aop = $this->lifegetPublicContent();
        $request = new AlipayUserInfoShareRequest ();
        $result = $aop->execute ( $request , $accessToken ); 

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        return $result->$responseNode;
    }
    
    public function refund($data){
		$aop = $this->getPublicContent(); 
		$data = json_decode($data);
		$request = new AlipayTradeRefundRequest ();
		$request->setBizContent("{" .
		"\"out_trade_no\":\"$data->out_order_no\"," .
		"\"trade_no\":\"$data->trade_no\"," .
		"\"refund_amount\":$data->refund_money," .
		"\"refund_currency\":\"CNY\"," .
		"\"refund_reason\":\"正常退款\"" .
		
		"  }");
		$token = $data->token;
        $result = $aop->execute ($request, null, $token);
		
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		return $result->$responseNode;
	}
}